library(JavaGD)
library(grid)   
library(lattice)    
library(latticeExtra)
library(reshape)
library(RColorbrewer)
	
selected_dat = read.csv("cell-output.csv", skip=6, header = TRUE)
dat = read.csv("cell-output.csv", skip=6, header = TRUE)
print(str(dat))
n = 2

plot_viz_x <- function(xName, yName, condXName, grouping, heatmapMetric) {
	write.table(dat, "all-cell-output.csv")
	print('1 Make syntactically valid names')
	xName = make.names(xName)
	yName = make.names(yName)
	condXName = make.names(condXName)
	print ("c(nchar(heatmapMetric) ",nchar(heatmapMetric))
	if (nchar(heatmapMetric) == 0) 
	{
		heatmapMetric = "1"
	}

	## TODO: What will happen when n > 1
	print('1.5 Get Colors"')
	if (n == 1)
	{ myColors <<- brewer.pal(3,"Set1")[1] }
	if (n == 2)
	{ myColors <<- brewer.pal(3,"Set1")[1:2] }
	if (n < 9 && n > 2 )
	{ myColors <<- brewer.pal(n,"Set1") }
	
	print('2 Extract column with names from selected_dat')
	x  <-  selected_dat[[xName]]
	y  <-  selected_dat[[yName]]	
	condX = selected_dat[[condXName]]

	print('3 Set default trellist parameters')	
	trellis.par.set( axis.line = list(lwd=1) )
	trellis.par.set("strip.border" = list(col="black"))

	print('4 Parse grouping and heatmapMetric')
	heatmap_metric_parsed = parse(text = heatmapMetric)	
	if (nchar(grouping) != 0)
	{ grouping_parsed = parse(text = grouping)}
	else
	{ grouping_parsed = NULL}
	
	print('5 plot_viz_x xyplot')	
	v =	xyplot(y ~ x | condX,
			data = selected_dat,
			#groups =X.run.number.,
			groups = dataset,
			alpha = 0.5,
			#type = "p",
		    pch = ".",
			layout = c(0,eval(parse(text=c("nlevels(shingle(dat$",condXName,"))")))),
			distribute.type = TRUE,
			#groups = grouping_parsed,
			scales = list(draw = FALSE),
			type = "p",
			#strip = FALSE,
			strip = strip.custom(
					strip.levels = TRUE,
					strip.names = FALSE,
					sep = ' * ',
					bg = 'white',
					fg = 'white'),
			#col = brewer.pal(max(selected_dat$dataset),"Set1")[selected_dat$dataset],
			#col = c(#FF0000","#00FF00","#0000FF"),
			xlab = condXName,
			ylab = xName,
			main = yName,
			aspect =eval(parse(text=c("nlevels(shingle(dat$",condXName,"))")))/2,
			auto.key = list(space = "right"),

			par.settings=list(
					border = TRUE,
					layout.widths=list(left.padding=0, right.padding=0),
					layout.heights=list(top.padding=0, bottom.padding=0)
			),
			#scales = list(x = list(relation="free")),
#			panel = function(...) {
#				#panel.groups =  dat$X.run.number.
#				l <- list(...) # Put the arguments in a (named) list
#				#str(l)
#				#print(summary(l))
#				print("*******************************************")
#				#print("selected_dat[l$subscript,]$X.run.number")
#				print("l$subscript")			
#				print(l$subscript)
#				print("selected_dat[1]$X.run.number")
#				print(selected_dat[l$subscript,]$dataset)
#				print("brewer.pal(max(selected_dat$X.run.number),\"Set1\")[selected_dat[l$subscript,]$X.run.number]")
#				print(brewer.pal(max(selected_dat$dataset),"Set1")[selected_dat[l$subscript,]$dataset])
#				col.line = brewer.pal(max(selected_dat$dataset),"Set1")[selected_dat[l$subscript,]$dataset]
#				#panel.groups= selected_dat[l$subscript,]$X.run.number.
#				#TODO: handle statements with with syntactically invalid names 
#				#for (i in seq(along = l)) 
#				#{
#				#		cat("Argument ", i, " name:", names(l)[i], "Value:", l[[i]], "\n")		   
#				#}
#				panel <- data.frame(l[1:3])	
#				avg = eval(heatmap_metric_parsed)
#				# Put gray squares or dotted texture if NaN
#				if (!is.nan(avg))
#				{
#					panel.fill(col = rgb(avg,avg,avg))
#					panel.superpose( ...)
#				}
#				else
#				{
#					panel.grid(col = "gray",lty = 1)
#				}
#			}
	)
	print(v)
	viz <<- v
}
plot_viz_x('X.step.','count.turtles','initial.number.sheep','','1')